package cn.jiyun.service.impl;

import cn.jiyun.dao.CheckGroupDao;
import cn.jiyun.entity.PageResult;
import cn.jiyun.entity.QueryPageBean;
import cn.jiyun.pojo.CheckGroup;
import cn.jiyun.serivce.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupDao.findList(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
//        1、添加基本信息
        checkGroupDao.add(checkGroup);
        // 2、添加中间表
        for (Integer checkitemId : checkitemIds) {
            checkGroupDao.addGI(checkitemId,checkGroup.getId());
        }
    }

    @Override
    public List<Integer> findCheckItemIdsByGid(Integer id) {
        return checkGroupDao.findCheckItemIdsByGid(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //1、修改基本信息
        checkGroupDao.edit(checkGroup);
        //2、删除中间表
        checkGroupDao.delGIByGid(checkGroup.getId());
        //3、添加中间表
        for (Integer checkitemId : checkitemIds) {
            checkGroupDao.addGI(checkitemId,checkGroup.getId());
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.finAll();
    }

}
